package com.weitu.environmental.solidWaste.service.impl;

import com.weitu.environmental.solidWaste.entity.SolidWasteApply;
import com.weitu.environmental.solidWaste.mapper.EntrustDisposeMapper;
import com.weitu.environmental.solidWaste.mapper.ExitDisposeMapper;
import com.weitu.environmental.solidWaste.mapper.SolidWasteApplyMapper;
import com.weitu.environmental.solidWaste.mapper.UnitDisposeMapper;
import com.weitu.environmental.solidWaste.service.SolidWasteApplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @author zhangqingxi 2017-04-17
 */
@Service
public class SolidWasteApplyServiceImpl implements SolidWasteApplyService {
    private final SolidWasteApplyMapper solidWasteApplyMapper;
    private final UnitDisposeMapper unitDisposeMapper;
    private final EntrustDisposeMapper entrustDisposeMapper;
    private final ExitDisposeMapper exitDisposeMapper;

    @Autowired
    public SolidWasteApplyServiceImpl(SolidWasteApplyMapper solidWasteApplyMapper, UnitDisposeMapper unitDisposeMapper, EntrustDisposeMapper entrustDisposeMapper, ExitDisposeMapper exitDisposeMapper) {
        this.solidWasteApplyMapper = solidWasteApplyMapper;
        this.unitDisposeMapper = unitDisposeMapper;
        this.entrustDisposeMapper = entrustDisposeMapper;
        this.exitDisposeMapper = exitDisposeMapper;
    }

    @Override
    public List<SolidWasteApply> findByParam(Map paramMap) {
        return solidWasteApplyMapper.findByParam(paramMap);
    }

    @Override
    public Integer findByParamCount(Map paramMap) {
        return solidWasteApplyMapper.findByParamCount(paramMap);
    }

    @Override
    public SolidWasteApply findById(String id) {
        return solidWasteApplyMapper.findById(id);
    }

    @Transactional
    @Override
    public Integer add(Map paramMap) {
        return solidWasteApplyMapper.add(paramMap);
    }

    @Transactional
    @Override
    public Integer updateById(Map paramMap) {
        return solidWasteApplyMapper.updateById(paramMap);
    }

    @Transactional
    @Override
    public Integer disposeById(List<Map> unitDisposeList, List<Map> entrustDisposeList, List<Map> exitDisposeList, Map paramMap, String id) {

        if (unitDisposeList.size() > 0) {
            //执行删除 部门内部数据
            unitDisposeMapper.deleteBySwaId(id);
            //执行添加 部门内部数据
            unitDisposeMapper.add(unitDisposeList);
        }

        if (entrustDisposeList.size() > 0) {
            //执行删除 外委单位
            entrustDisposeMapper.deleteBySwaId(id);
            //执行添加 外委单位
            entrustDisposeMapper.add(entrustDisposeList);
        }

        if (exitDisposeList.size() > 0) {
            //执行删除 出口国
            exitDisposeMapper.deleteBySwaId(id);
            //执行添加 出口国
            exitDisposeMapper.add(exitDisposeList);
        }


        return solidWasteApplyMapper.updateById(paramMap);
    }
}
